package cc.ufinity.networkdiagnose.manager;

import cc.ufinity.networkdiagnose.util.StringUtils;
import cc.ufinity.networkdiagnose.widget.MyMobileCfgWidget;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

/**
 * 获取手机信号及网络类型
 * 
 * @author ZR
 * 
 */
public final class TelManager extends AbstractManager {
    private TelephonyManager tm;
    private Context ctx;
    private MyPhoneStateListener phoneStateListener = null;
    private String strSignalStrength;
    private int cdmaDbM;
    private GsmCellLocation gsm = null;

    private int ber;
    private int signalStrength;

    public int getSignalStrength() {
        return signalStrength;
    }

    public void setCdmaDbM(int cdmaDbM) {
        this.cdmaDbM = cdmaDbM;
    }

    public int getCdmaDbM() {
        return cdmaDbM;
    }

    private int evdoDbm;

    public void setEvdoDbm(int evdoDbm) {
        this.evdoDbm = evdoDbm;
    }

    public int getEvdoDbm() {
        return evdoDbm;
    }

    public TelManager(Context context) {
        this.ctx = context;
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        phoneStateListener = new MyPhoneStateListener();
        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        String networkType = getNetworkType();
        if ("EDGE".equals(networkType) || "HSDPA".equals(networkType)) {
            gsm = (GsmCellLocation) tm.getCellLocation();
        }
        tm.listen(new PhoneStateListener() {
            @Override
            public void onDataConnectionStateChanged(int state) {
                switch (state) {
                case TelephonyManager.DATA_DISCONNECTED:// 网络断开
                    // Toast.makeText(ctx, "网络已断开", 10000).show();
                    break;
                case TelephonyManager.DATA_CONNECTING:// 网络正在连接
                    // Toast.makeText(ctx, "正在连接网络", 10000).show();
                    break;
                case TelephonyManager.DATA_CONNECTED:// 网络连接上
                    // Toast.makeText(ctx, "网络连接成功", 10000).show();
                    break;
                }
            }

        }, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    }

    public int getLac() {
        return (gsm != null) ? gsm.getLac() : 0;
    }

    public int getCI() {
        return (gsm != null) ? gsm.getCid() : 0;
    }

    public String getPhoneNumber() {
        String tel = tm.getLine1Number();
        if (StringUtils.isEmpty(tel)) {
            SharedPreferences settings = ctx.getSharedPreferences(MyMobileCfgWidget.PREFS_NAME, 0);
            tel = settings.getString(MyMobileCfgWidget.PREFS_KEY, "");
            Log.d("getPhoneNumber", "配置的本机号码:"+tel);
        }
        return tel;
    }

    public String getNetworkType() {
        String s = null;
        int a = tm.getNetworkType();
        if (a == TelephonyManager.NETWORK_TYPE_1xRTT) {
            s = "1xRTT";
        } else if (a == TelephonyManager.NETWORK_TYPE_CDMA) {
            s = "CDMA";
        } else if (a == TelephonyManager.NETWORK_TYPE_EDGE) {
            s = "EDGE";
        } else if (a == TelephonyManager.NETWORK_TYPE_EVDO_0) {
            s = "EVD0_0";
        } else if (a == TelephonyManager.NETWORK_TYPE_EVDO_A) {
            s = "EVD0_A";
        } else if (a == TelephonyManager.NETWORK_TYPE_GPRS) {
            s = "GPRS";
        } else if (a == TelephonyManager.NETWORK_TYPE_HSDPA) {
            s = "HSDPA";
        } else if (a == TelephonyManager.NETWORK_TYPE_HSPA) {
            s = "HSPA";
        } else if (a == TelephonyManager.NETWORK_TYPE_HSUPA) {
            s = "HSUPA";
        } else if (a == TelephonyManager.NETWORK_TYPE_UMTS) {
            s = "UMTS";
        } else {
            s = "UNKNOW";
        }
        return s;
    }

    public int getBER() {
        return ber;
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        public void onSignalStrengthsChanged(SignalStrength sStrength) {
            super.onSignalStrengthsChanged(sStrength);// 调用超类的该方法，在网络信号变化时得到回答信号
            StringBuffer s = new StringBuffer();
            ber = sStrength.getGsmBitErrorRate();
            signalStrength = sStrength.getGsmSignalStrength();
            s.append("BER = ").append(ber).append(';');
            s.append("Signal Strength = ").append(signalStrength);
            setStrSignalStrength(s.toString());
            setEvdoDbm(sStrength.getEvdoDbm());
            setCdmaDbM(sStrength.getCdmaDbm());
            sendMsg(MessageWhat.TEL_STATE_SUCCESS);
        }
    }

    public String getStrSignalStrength() {
        return strSignalStrength;
    }

    public void setStrSignalStrength(String strSignalStrength) {
        this.strSignalStrength = strSignalStrength;
    }

}
